package ch.fhnw.qualiscan.application.modals;

import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.QuestionModel;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.DomainText;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.db.pojos.QuestionText;
import ch.fhnw.qualiscan.core.helpers.UserSessionHelper;
import ch.fhnw.qualiscan.core.helpers.TextHelper;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class Chart extends Modal {
	
	private QuestionModel questionModel;
	private UserSessionHelper sessionHelper;
	private TextHelper textHelper;
	private DomainModel domainModel;
	
	public Chart(String apiUrl, int questionId, int domainId) {
		setWidth("830px");
		setHeight("550px");
		
		// toolbar title of the chart
		setCaption(TM.get("chart.detail.title"));
		
		if (questionId != 0) {
			textHelper = new TextHelper();
			sessionHelper = new UserSessionHelper();
			questionModel = new QuestionModel();
			Question question = questionModel.getQuestionById(questionId);
			QuestionText qt = questionModel.getQuestionText(sessionHelper.getUserSession().getLanguage(), question);
			if (question.getParentQuestion() != null) {
				QuestionText qtParent = questionModel.getQuestionText(sessionHelper.getUserSession().getLanguage(), question.getParentQuestion());
				Label parentQuestionLabel = textHelper.p(qtParent.getText());
				this.addComponent(parentQuestionLabel);
			}
			
			Label questionLabel = textHelper.p(qt.getText());
			VerticalLayout subquestion = new VerticalLayout();
			subquestion.addComponent(questionLabel);
			subquestion.setMargin(false, false, false, true);
			this.addComponent(subquestion);
		}
		
		if (domainId != 0) {
			textHelper = new TextHelper();
			sessionHelper = new UserSessionHelper();
			domainModel = new DomainModel();
			Domain domain = domainModel.getDomainById(domainId);
			DomainText dt = domainModel.getDomainText(domain, sessionHelper.getUserSession().getLanguage());
			
			Label domainLabel = textHelper.p(dt.getText());
			this.addComponent(domainLabel);
		}
		
		addFlashChart(apiUrl);
	}
	
	public void addFlashChart(String apiUrl) {
		Embedded e = new Embedded(null, new ThemeResource("../qualiscan/charts/detailChart.swf?jsondataurl="+apiUrl));
		e.setMimeType("application/x-shockwave-flash");
		
		e.setHeight("400px");
		e.setWidth("800px");
		addComponent(e);
	}
	
}
